package com.example.demo.heartbeat;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.concurrent.TimeUnit;

/**
 * Title: HeartBeatClientFilter
 * Description: Netty客户端 过滤器
 * Version:1.0.0
 *
 * @author kh
 * @date 2021-08-10 16:39:56
 */
public class HeartBeatClientFilter extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline ph = ch.pipeline();
        /*
         * 解码和编码，应和服务端一致
         * */
//        ph.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
        // 入参说明: 读超时时间、写超时时间、所有类型的超时时间、时间格式
        // 因为服务端设置的超时时间是60秒，所以设置55秒 (此处设置心跳包多久发送一次)
        ph.addLast(new IdleStateHandler(0, 55, 0, TimeUnit.SECONDS));
//        ph.addLast("decoder", new StringDecoder());
        ph.addLast("encoder", new HeartBeatEncode());
        //客户端的逻辑
        ph.addLast("handler", new HeartBeatClientHandler());
    }
}
